package org.spring.blog.controller.admin;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.spring.blog.pojo.Article;
import org.spring.blog.response.ResponseResult;
import org.spring.blog.response.ResponseState;
import org.spring.blog.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@CrossOrigin
@RestController
@RequestMapping("/admin/article")
public class ArticleAdminApi {

    @Autowired
    private IArticleService articleService;

    /**
     * 增加文章
     *
     * @param _article 文章对象
     * @return 自定义返回对象
     */
    @ApiOperation("增加文章")
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.user_not_login_code, message = ResponseState.user_not_login_message),
            @ApiResponse(code = ResponseState.input_data_not_complete_code, message = ResponseState.input_data_not_complete_message),
            @ApiResponse(code = ResponseState.article_summary_length_too_long_code, message = ResponseState.article_summary_length_too_long_message),
            @ApiResponse(code = ResponseState.article_title_length_too_long_code, message = ResponseState.article_title_length_too_long_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PreAuthorize("@permission.admin()")
    @PostMapping
    public ResponseResult insert(@RequestBody Article _article) {
        return articleService.insert(_article);
    }

    /**
     * 删除文章
     *
     * @param _Id id
     * @return 自定义返回对象
     */
    @ApiOperation("删除文章")
    @ApiResponses({
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PreAuthorize("@permission.admin()")
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable("id") String _Id) {
        return articleService.delete(_Id);
    }

    /**
     * 查询文章
     *
     * @param _Id 文章id
     * @return 自定义返回对象
     */
    @ApiOperation("查询文章")
    @ApiResponses({
            @ApiResponse(code = ResponseState.user_not_login_code, message = ResponseState.user_not_login_message),
            @ApiResponse(code = ResponseState.not_support_operation_code, message = ResponseState.not_support_operation_message),
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @CrossOrigin
    @GetMapping("/{id}")
    public ResponseResult query(@PathVariable("id") String _Id, HttpServletResponse _response) {
        return articleService.queryByIdForAdmin(_Id);
    }

    /**
     * 更新文章
     *
     * @param _Id      文章id
     * @param _article 文章对象
     * @return 自定义返回对象
     */
    @ApiOperation("更新文章")
    @ApiResponses({
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PreAuthorize("@permission.admin()")
    @PutMapping("/{id}")
    public ResponseResult update(@PathVariable("id") String _Id, @RequestBody Article _article) {
        return articleService.update(_Id, _article);
    }

    /**
     * 列表获取文章
     *
     * @param page       页码
     * @param size       页面大小
     * @param keyword    关键词
     * @param categoryId 分类id
     * @return 自定义返回对象
     */
    @ApiOperation("列表获取文章")
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PreAuthorize("@permission.admin()")
    @GetMapping("/list/{page}/{size}")
    public ResponseResult list(@PathVariable int page,
                               @PathVariable int size,
                               @RequestParam(value = "keyword", required = false) String keyword,
                               @RequestParam(value = "state", required = false) String state,
                               @RequestParam(value = "categoryId", required = false) String categoryId) {

        return articleService.list(page, size, keyword, categoryId, state);
    }

    /**
     * 标记删除
     *
     * @param id 文章id
     * @return 自定义返回对象
     */
    @ApiOperation("标记删除")
    @ApiResponses({
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PutMapping("/state/{id}")
    public ResponseResult deleteArticleByState(@PathVariable("id") String id) {
        return articleService.deleteArticleByState(id);
    }

    /**
     * 置顶文章
     *
     * @param id 文章id
     * @return 自定义返回对象
     */
    @ApiOperation("置顶文章")
    @ApiResponses({
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PreAuthorize("@permission.admin()")
    @PutMapping("/top/{id}")
    public ResponseResult topArticle(@PathVariable("id") String id) {
        return articleService.topArticle(id);
    }

    /**
     * 获取文章数量
     *
     * @return 自定义返回对象
     */
    @ApiOperation("获取文章数量")
    @ApiResponses(@ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message))
    @GetMapping("/count")
    public ResponseResult getArticleCount() {
        return articleService.getArticleCount();
    }
}
